/* ***** BEGIN LICENSE BLOCK *****
 * Version: NPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Netscape Public License
 * Version 1.1 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.mozilla.org/NPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is lutsko.com code.
 *
 * The Initial Developer of the Original Code is 
 * James Lutsko.
 * Portions created by the Initial Developer are Copyright (C) 2003
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the NPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the NPL, the GPL or the LGPL.
 *
 * ***** END LICENSE BLOCK ***** */


package autoteam;

import java.io.*;

/**
 * The Virtue class abstracts all of the comparable 
 * quantities. Its name should be changed to "Skill"
 * or some such thing.
 *
 * Its role is twofold: to provide a slot for 
 * holding the total skill and, more importantly,
 * to handle the job of comparing two children
 * according to the current criterion.
 * All of the latter functions are handled by static
 * members and methods.
 *
 * Note that when sorting, the first is the most
 * desirable, second is next, etc.
 *
 *@see Child
 *@see Drafter
 */


public class Virtue implements Serializable, Comparable {

    public Virtue() {
    	skill = null;
    	weight = null;
    }

    public Double[] skill;
    public Double[] weight;

    public double totalSkill;

    public int experience = 0;
    static public int currentSkill = -1;


    public Double getSkill(int j){return skill[j];}
    
    public Double getWeight(int j){return weight[j];}

    /** 
     * This is the index of the current skill
     * used for sorting. If this is negative,
     * the total is used.
     */
    static int[] skillOrder = null;

    static int[] tieBreakers = null;
    
    //    static int skillNumer = 0;


    private void readObject(ObjectInputStream stream)
        throws IOException, ClassNotFoundException
    {
	stream.defaultReadObject();
	//	if(skill != null) skillNumber = skill.length;
    }

    public int compareTo(Object o)
    {
    	Virtue V = (Virtue) o;
    	int r = 0;
    	if (skillOrder != null)
    	{
    		// 1. Simple Draft w/ tie-breakers
    		if (skillOrder[0] == -1)
    		{
    			if (totalSkill == V.totalSkill)
    	    	{
    				for (int i = 1; i < skillOrder.length; i++)
    				{
    		    		int s = skillOrder[i];
    		    		if (s >= 0) r = (skill[s] > V.skill[s] ? -1 : 1);    		    		
   			    		if (r != 0) return r;
   			    	}
    	    	}
    			else
    				r = (totalSkill > V.totalSkill ? -1 : 1);
    		}
    		
    		// 2. Positional Draft
    		else
    		{
    			for (int i = 0; i < skillOrder.length; i++)
				{
		    		int s = skillOrder[i];
		    		if (s >= 0) r = -skill[s].compareTo(V.skill[s]);
		    		if (r != 0) return r;
		    		else
		    		{
		    			int t = tieBreakers[i];
		    			r = -skill[t].compareTo(V.skill[t]);
		    			if (r != 0) return r;
		    		}
			    }
    		}
    	}
    	
    	// 3. Simple Draft w/o tie-breakers
    	if (totalSkill == V.totalSkill)
    	{
  			if (experience == V.experience) r = 0;
  			else r = (experience > V.experience ? -1 : 1);	// use experience to break ties
    	}
    	else r = (totalSkill > V.totalSkill ? -1 : 1);
    	
    	return r;
    }
    
    
    /* author's compareTo method
    public int compareTo(Object o)
    {
	Virtue V = (Virtue) o;
	int r = 0;
	
	//	if(!currentSkill)
	//	    {
	if(skillOrder != null)
	    for(int i=0;i<skillOrder.length;i++)
		{
		    int s = skillOrder[i];
		    if(s >= 0) {
			r = -skill[s].compareTo(V.skill[s]);
			if(i == 0) currentSkill = s;
		    }
		    //		    else r = totalSkill.compareTo(V.totalSkill);
		    if(r != 0) return r;
		    //	    }
		    //	    } else {
		    //		int s = currentSkill;
		    //		if(s >= 0) r = -skill[s].compareTo(V.skill[s]);
		    //		else r = 0;
		}
		    
	// always default to the total???
	//	if(r == 0)
	//   if(totalSkill == V.totalSkill)
	//	{
	//	    if(experience == V.experience)
	//		r = 0;
	//	    else r = (experience > V.experience ? -1 : 1);
	//	} else r = (totalSkill > V.totalSkill ? -1 : 1);
	//
	return r;
    }
	*/
	
    static final long serialVersionUID = 3964377044708581372L;
}
